package com.example.coding_problem.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.coding_problem.Life_assistant_SQL;
import com.example.coding_problem.Life_assistant_list;
import com.example.coding_problem.R;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

import static android.support.constraint.Constraints.TAG;

public class Life_LineChart1 extends Fragment {
    private int max;
    private int min;
    private ArrayList<BarEntry>barEntries;
    private Life_assistant_SQL dbHelper;
    private BarChart life_check_bc;
    private List<Life_assistant_list> life_bar_list = new ArrayList<>();
    String[] name={"","03", "06", "09", "12", "15", "18", "21","24", "27", "30","33","36","39","42","45","48","51","54","57","60"};
    public Life_LineChart1() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_life__line_chart1, container, false);
        initview(view);
        initSQL();
        initLineChart(view);
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable,3000);
        return view;
    }
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable(){
        @Override
        public void run() {
            life_bar_list = new ArrayList<>();
            initSQL();
            life_check_bc.invalidate();
            handler.postDelayed(this,3000);
        }
    };
    private void initview(View view){
        dbHelper = new Life_assistant_SQL(getActivity(),"BookStore.db",null,2);
        life_check_bc=view.findViewById(R.id.life_check_bc);
    }
    private void initLineChart(View view) {
        //柱状图的阴影
        life_check_bc.setDrawBarShadow(false);
        //设置柱状图Value值显示在柱状图上方 true 为显示上方，默认false value值显示在柱状图里面
        life_check_bc.setDrawValueAboveBar(true);
        //Description Label 是否可见
        life_check_bc.getDescription().setEnabled(false);
        // 设置最大可见Value值的数量 针对于ValueFormartter有效果
        life_check_bc.setMaxVisibleValueCount(10);
        // 二指控制X轴Y轴同时放大
        life_check_bc.setPinchZoom(false);
        //是否显示表格背景颜色
        life_check_bc.setDrawGridBackground(false);
        XAxis xAxis = life_check_bc.getXAxis();
        //设置X轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //X轴纵向分割线，一般不设置显示
        xAxis.setDrawGridLines(false);
        //X轴最大坐标
//        xAxis.setAxisMaximum(60f);
        //X轴最小坐标
        xAxis.setAxisMinimum(0.5f);
        // X轴显示Value值的精度，与自定义X轴返回的Value值精度一致
        xAxis.setGranularity(1f);
        //X轴横坐标显示的数量
        xAxis.setLabelCount(20);
        //自定义x轴
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return name[(int) value];
            }
        });
        //Y左边轴
        YAxis leftAxis = life_check_bc.getAxisLeft();
        //设置Y左边轴显示的值 label 数量
        leftAxis.setLabelCount(8, false);
        //设置值显示的位置，我们这里设置为显示在Y轴外面
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        //设置Y轴 与值的空间空隙 这里设置30f意为30%空隙，默认是10%
        leftAxis.setSpaceTop(30f);
        //设置Y轴最小坐标和最大坐标
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(210f);

        //Y轴右边轴的设置，跟左边轴类似
        YAxis rightAxis = life_check_bc.getAxisRight();
        rightAxis.setDrawGridLines(false);
        //隐藏右边y轴
        rightAxis.setEnabled(false);
        rightAxis.setLabelCount(8, false);
        rightAxis.setSpaceTop(30f);
        rightAxis.setAxisMinimum(0f);
        rightAxis.setAxisMaximum(10f);
        // 设置表格标示的位置
        Legend l = life_check_bc.getLegend();
        //标示坐落再表格左下方
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        //标示水平朝向
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //标示在表格外
        l.setDrawInside(false);
        //样式
        l.setForm(Legend.LegendForm.SQUARE);
        //字体
        l.setFormSize(15f);
        //大小
        l.setTextSize(15f);
    }
    private void initSQL(){
        SQLiteDatabase db= dbHelper.getWritableDatabase();
        Cursor infol=db.query("Book",null,null,null,null,null,null);
        int i=0;
        life_bar_list = new ArrayList<>();
        while (infol.moveToNext()){
            Life_assistant_list data=new Life_assistant_list();
            i++;
            data.set_$Pm256(Integer.parseInt(infol.getString(infol.getColumnIndex("pm2_5"))));
            life_bar_list.add(data);
        }
            barEntries=new ArrayList<>();
                for (int j=0;j<life_bar_list.size();j++){
                barEntries.add(new BarEntry(j+1,life_bar_list.get(j).get_$Pm256()));
                    max=life_bar_list.get(0).get_$Pm256();
                    min=life_bar_list.get(0).get_$Pm256();
                    for (int a=0;a<barEntries.size();a++){
                        if (min>life_bar_list.get(a).get_$Pm256()){
                            min=life_bar_list.get(a).get_$Pm256();
                        }
                        if (max<life_bar_list.get(a).get_$Pm256()){
                            max=life_bar_list.get(a).get_$Pm256();
                        }
                    }
            }
            BarDataSet set1=new BarDataSet(barEntries,"过去一分钟空气质量最差值:"+min);
            set1.setValueTextSize(10);
            BarData set2=new BarData(set1);
            life_check_bc.setData(set2);//设置数据
            life_check_bc.invalidate();
    }
}
